﻿#region using
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;

using XmlGenSharp.Logic.Main.Configuration;
#endregion

namespace XmlGenSharp.Logic.Main
{
  public class AnalyzerContext : IAnalyzerContext
  {
    // Constructors

    #region AnalyzerContext(BindingAnalyzer bindingAnalyzer)
    public AnalyzerContext(BindingAnalyzer bindingAnalyzer)
    {
      BindingAnalyzer = bindingAnalyzer;
    }
    #endregion

    // Public Properties

    #region BindingAnalyzer
    public BindingAnalyzer BindingAnalyzer
    {
      get;
      protected set;
    }
    #endregion

    #region Errors
    public IErrorList Errors
    {
      get
      {
        if (_errors == null)
          _errors = new ErrorList();

        return _errors;
      }
    }
    private IErrorList _errors = null;
    #endregion

    // Public Methods

    #region FoundComplexType(XmlSchemaComplexType complexType)
    public virtual void FoundComplexType(XmlSchemaComplexType complexType)
    {
    }
    #endregion

    #region FoundSimpleType(XmlSchemaSimpleType simpleType)
    public virtual void FoundSimpleType(XmlSchemaSimpleType simpleType)
    {
    }
    #endregion

    #region FoundEnumeration(EnumerationInfo enumerationInfo)
    public virtual void FoundEnumeration(EnumerationInfo enumerationInfo)
    {
    }
    #endregion

    #region FoundEnumerationElement(EnumerationInfo enumerationInfo, Element element)
    public virtual void FoundEnumerationElement(EnumerationInfo enumerationInfo, Element element)
    {
    }
    #endregion

    #region AddError(string sourceClass, string sourceMember, string message)
    public void AddError(string sourceClass, string sourceMember, string message)
    {
      Error error = new Error(sourceClass, sourceMember, message);
      Errors.Add(error);
    }
    #endregion

    #region OnBeforeAnalyzeFromJobFile()
    public virtual void OnBeforeAnalyzeFromJobFile()
    {
    }
    #endregion

    #region OnAfterAnalyzeFromJobFile()
    public virtual void OnAfterAnalyzeFromJobFile()
    {
    }
    #endregion

    #region OnBeforeAnalyzeFromXsd()
    public virtual void OnBeforeAnalyzeFromXsd()
    {
    }
    #endregion

    #region OnAfterAnalyzeFromXsd()
    public virtual void OnAfterAnalyzeFromXsd()
    {
    }
    #endregion

    #region OnBeforeAnalyzeFromEnumerationInfo()
    public virtual void OnBeforeAnalyzeFromEnumerationInfo()
    {
    }
    #endregion

    #region OnAfterAnalyzeFromEnumerationInfo()
    public virtual void OnAfterAnalyzeFromEnumerationInfo()
    {
    }
    #endregion

    // Protected Methods

    #region AppendJobInfo(StringWriter sw)
    protected void AppendJobInfo(StringWriter sw)
    {
      Job job = new Job(BindingAnalyzer.InputFileName);

      string fullPath = job.Schema.File;
      if (!Path.IsPathRooted(fullPath))
        fullPath = Path.Combine(Path.GetDirectoryName(BindingAnalyzer.InputFileName), fullPath);

      sw.WriteLine("#region Code Generation Information");
      sw.WriteLine("// This file was auto-generated by XmlGen#");
      sw.WriteLine("// DO NOT DIRECTLY MODIFY THIS FILE.  Changes will be overwritten");
      sw.WriteLine("// the next time this file is generated.");
      sw.WriteLine();
      sw.WriteLine("// XmlGen# Job File Name: {0}", BindingAnalyzer.InputFileName);
      sw.WriteLine("// Schema File Name:      {0}", fullPath);
      sw.WriteLine("// CLR Namespace:         {0}", BindingAnalyzer.Namespace);
      sw.WriteLine("// Generated On:          {0}", DateTime.Now);
      sw.WriteLine("#endregion");
      sw.WriteLine();
    }
    #endregion

    #region AppendXsdInfo(StringWriter sw)
    protected void AppendXsdInfo(StringWriter sw)
    {
      sw.WriteLine("#region Code Generation Information");
      sw.WriteLine("// This file was auto-generated by XmlGen#");
      sw.WriteLine("// DO NOT DIRECTLY MODIFY THIS FILE.  Changes will be overwritten");
      sw.WriteLine("// the next time this file is generated.");
      sw.WriteLine();
      sw.WriteLine("// Schema File Name: {0}", BindingAnalyzer.InputFileName);
      sw.WriteLine("// Generated On:     {0}", DateTime.Now);
      sw.WriteLine("#endregion");
      sw.WriteLine();
    }
    #endregion

    #region AppendCommonPreText(StringWriter sw, string nmspace)
    protected void AppendCommonPreText(StringWriter sw, string nmspace)
    {
      sw.WriteLine("#region using");

      if (BindingAnalyzer.Config.UsingsSection == null ||
          !BindingAnalyzer.Config.UsingsSection.SuppressSystemUsings)
      {
        // Generate default System usings
        sw.WriteLine("using System;");
        sw.WriteLine("using System.Collections;");
        sw.WriteLine("using System.Collections.Generic;");
        sw.WriteLine("using System.Xml;");
        sw.WriteLine();
      }

      if (BindingAnalyzer.Config.UsingsSection == null ||
          !BindingAnalyzer.Config.UsingsSection.SuppressStandardUsings)
      {
        // Generate default other usings
        sw.WriteLine("using XmlGenSharp.Lib.Utility;");
      }

      if (BindingAnalyzer.Config.UsingsSection != null &&
          BindingAnalyzer.Config.UsingsSection.ExtraUsings != null)
      {
        foreach (Using u in BindingAnalyzer.Config.UsingsSection.ExtraUsings.Usings)
        {
          if (String.IsNullOrEmpty(u.Namespace))
            sw.WriteLine();
          else
            sw.WriteLine("using {0};", u.Namespace);
        }
      }

      sw.WriteLine("#endregion");

      sw.WriteLine();
      sw.WriteLine("#region pragmas");
      sw.WriteLine("#pragma warning disable 0219");
      sw.WriteLine("#endregion");
      sw.WriteLine();
      sw.WriteLine("namespace {0}", nmspace);
      sw.Write("{");
    }
    #endregion
  }
}
